// pages/my/my_shopDetail/index.js
const storeData=require("../../controllers/ControllerX").controller.storeData;

const areaList=require("../../controllers/ControllerX").controller.areaList;
const areaListChild=require("../../controllers/ControllerX").controller.areaListChild;

const controller=require("../../controllers/Controller").controller


const app=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeData:{
      
    },
    imgList:[],
    delImgList:[],
    hours:[
      ["00:00","00:30","01:00","01:30","02:00","02:30","03:00","03:30","04:00","04:30","05:00","05:30","06:00","06:30","07:00","07:30","08:00","08:30","09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30","18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30","22:00","22:30","23:30"],["24:00","00:30","01:00","01:30","02:00","02:30","03:00","03:30","04:00","04:30","05:00","05:30","06:00","06:30","07:00","07:30","08:00","08:30","09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30","18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30","22:00","22:30","23:30"]
    ],
    value:{},
    mydata:'',
    baseUrl:"https://www.xlshhr.com/beauty-industry",
  },


  //删除内部环境图
  delImg(e){
    let index=e.currentTarget.dataset.index
    let imgList=this.data.imgList
    imgList.splice(index,1)
    this.setData({
      imgList:imgList,
    })
  },


  //选择营业时间
  selectTime(e){
    let startArr=e.detail.value[0];
    let endArr=e.detail.value[1];
    let startTime=this.data.hours[0][startArr];
    let endTime=this.data.hours[1][endArr];
    this.setData({
      'value.businessTime':startTime+"-"+endTime
    })
    console.log(this.data.value.businessTime);
  },

   //获取商铺详情
   getStoreData(){
    let shopId=wx.getStorageSync('shopId');
    shopId=parseInt(shopId);
    let imgList=[]
    let delImgList=[]
    storeData({shopId:shopId}).then(res=>{
      res.data.imgList.forEach(item=>{
        imgList.push(item.image)
        delImgList.push(item.id)
      })
      this.setData({
        storeData:res.data,
        'value.rovinceName':res.data.rovinceName,
        'value.cityName':res.data.cityName,
        'value.districtName':res.data.districtName,
        'value.districtId':res.data.districtId,
        'value.shopName':res.data.shopName,
        'value.address':res.data.address,
        'value.adminName':res.data.adminName,
        'value.customer':res.data.customer,
        'value.businessTime':res.data.businessTime,
        'value.lng':res.data.lng,
        'value.lat':res.data.lat,
        'value.shopImg':res.data.shopImg,
        'value.shopId':res.data.id,
        'value.detail':res.data.detail,
        'value.delImgIds':'',
        'value.internalImgs':'',
        imgList:imgList,
        delImgList:delImgList
      })
    })
  },



  //获取省
  getAreaList(){
    areaList({leveltype:1}).then(res=>{
      let shen=res.data;
      let id=0;
      let shenList="list["+id+"]"
      this.setData({
        [shenList]:shen,
        shen:shen[0].name
      })
      this.getAreaListChildShi(res.data[0].id)
    })
  },

  //根据上层id获取市
  getAreaListChildShi(id){
    areaListChild({areaId:id}).then(res=>{
      let shi=res.data;
      let id=1;
      let shiList="list["+id+"]"
      this.setData({
        [shiList]:shi,
        shi:shi[0].name
      })
      this.getAreaListChildQu(res.data[0].id)
    })
  },

  //根据上层id获取区
  getAreaListChildQu(id){
    areaListChild({areaId:id}).then(res=>{
      let qu=res.data;
      let id=2;
      let quList="list["+id+"]"
      this.setData({
        [quList]:qu,
        qu:qu[0].name,
        quId:qu[0].id
      })
    })
  },

  change(e){
    this.setData({
      'value.rovinceName':this.data.shen,
      'value.cityName':this.data.shi,
      'value.districtName':this.data.qu,
      'value.districtId':this.data.quId
    })
    console.log(this.data.value);
  },

  columnChange(e){
    let column=e.detail.column;   //列
    let value=e.detail.value;     //行

    let id=this.data.list[column][value].id;
    if(column==0){
      this.setData({
        shen:this.data.list[column][value].name
      })
      this.getAreaListChildShi(id);
    }else if(column==1){
      this.setData({
        shi:this.data.list[column][value].name
      })
      this.getAreaListChildQu(id);
    }else{
      this.setData({
        qu:this.data.list[column][value].name,
        quId:this.data.list[column][value].id
      })
    }
  },


   //判断地理位置是否授权并授权
   mapState(){
    let that=this;
    wx.getSetting({
      success(res){
        if(!res.authSetting['scope.userLocation']||res.authSetting['scope.userLocation']==false){
          console.log(res);
          wx.authorize({
            scope:'scope.userLocation',
            success(res){
              console.log(res);
              wx.getLocation({
                type: 'wgs84',
                isHighAccuracy:true,
                success(res){
                  if(!that.data.value.lng){
                    that.setData({
                      'value.lng':res.longitude,
                      'value.lat':res.latitude
                    })
                  }
                }
              })
            }
          })
        }else{
          wx.getLocation({
            type: 'wgs84',
            isHighAccuracy:true,
            success(res){
              console.log(res);
              // console.log(res.latitude);
              // console.log(res.longitude);
              if(!that.data.value.lng){
                that.setData({
                  'value.lng':res.longitude,
                  'value.lat':res.latitude
                })
              }
            }
          })
        }
      }
    })
  },

   //跳转地图选择
   getMap(){
    let that=this
    wx.getSetting({
      success(res){
        if(res.authSetting['scope.userLocation']==true){
          wx.chooseLocation({
            latitude: that.data.value.lat?that.data.value.lat:'',
            longitude:that.data.value.lng?that.data.value.lng:'',
            success(e){
              console.log(e)
              that.setData({
                'value.lat':e.latitude,
                'value.lng':e.longitude,
                'value.address':e.name
              })
            }
          })
        }else{
          wx.openSetting({
            withSubscriptions: true,
          })
        }
      }
    })
  },

  //获取门店名称
  getShopName(e){
    this.setData({
      'value.shopName':e.detail.value
    })
  },

  //获取门店负责人名称
  getAdminName(e){
    this.setData({
      'value.adminName':e.detail.value
    })
  },

  //获取门店负责人名称
  getCustomer(e){
    this.setData({
      'value.customer':e.detail.value
    })
  },

  //更新门店信息
  updateShopMsg(){
    if(!this.data.value.shopName||!this.data.value.address||!this.data.value.adminName||!this.data.value.customer){
      console.log(this.data.value);
      wx.showToast({
        icon:"none",
        title: '请完善信息',
      })
      return
    }
    this.setData({
      'value.delImgIds':this.data.delImgList.join(),
      'value.internalImgs':this.data.imgList.join()
    })
    controller.updateShopMsg(this.data.value).then(res=>{
        wx.showToast({
          title: '操作成功',
        })
        this.getStoreData()
    })
  },

  //跳转行业标签
  jumpTags(){
    wx.navigateTo({
      url: '/pages/settlement/tags/index',
    })
  },

  //获取店铺介绍
  getShopDetail(e){
    this.setData({
      'value.detail':e.detail.value
    })
  },

  //内部环境图片上传
  uploadImg(){
    let list=this.data.imgList;
    if(list.length<9){
      let that=this;
      wx.chooseImage({
        success (res) {
          const tempFilePaths = res.tempFilePaths;
          const tempFiles=res.tempFiles;
          for(let i=0;i<9-that.data.imgList.length;i++){
            wx.uploadFile({
              url: that.data.baseUrl+'/api/img/uploadFile', 
              filePath: tempFilePaths[i],
              name: 'image',
              formData: {
                'sessionId':that.data.token 
              },
              success (res){
                console.log(res);
                // const data = res.date
                let object={}
                let src=JSON.parse(res.data)
                object.image=
                list.push(src.data);
              that.setData({
                imgList:list
              })
              }
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getStoreData();
    this.mapState();
    this.getAreaList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.mydata){
      this.setData({
        'storeData.label':this.data.mydata,
        'value.label':this.data.mydata
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }

})